package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.MaintainTask;
import com.neusoft.neuiotms.dcom.model.QMaintainTask;
import com.neusoft.neuiotms.dcom.repository.CustomizedMaintainTaskRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedMaintainTaskRepositoryImpl extends AbstractRepository
        implements CustomizedMaintainTaskRepository {

    @Override
    public Long getCount(final Searchable searchable) {
        final QMaintainTask qMaintainTask = QMaintainTask.maintainTask;
        final JPAQuery<MaintainTask> query = this.query().selectFrom(qMaintainTask);
        final BooleanBuilder where = this.buildWhere(searchable);
        query.where(where);
        return query.fetchCount();
    }

    @Override
    public List<MaintainTask> search(final Searchable searchable) {
        final QMaintainTask qMaintainTask = QMaintainTask.maintainTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<MaintainTask> query = this.query().selectFrom(qMaintainTask);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<MaintainTask> search(final Pageable pageable, final Searchable searchable) {
        final QMaintainTask qMaintainTask = QMaintainTask.maintainTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<MaintainTask> query = this.query().selectFrom(qMaintainTask);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QMaintainTask qMaintainTask = QMaintainTask.maintainTask;

        if (!StringUtils.isEmpty(searchable.getStrValue("planId"))) {
            where.or(super.equalsLong(qMaintainTask.Plan.id, searchable, "planId"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("ruleId"))) {
            where.or(super.equalsLong(qMaintainTask.rule.id, searchable, "ruleId"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.or(super.containsStr(qMaintainTask.name, searchable, "name"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("code"))) {
            where.or(super.containsStr(qMaintainTask.code, searchable, "code"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("source"))) {
            where.and(super.containsStr(qMaintainTask.source.stringValue(), searchable, "source"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("startDateTime"))) {
            where.and(super.afterOrEqualsDate(qMaintainTask.startDateTime, searchable, "startDateTime"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("endDateTime"))) {
            where.and(super.beforeOrEqualsDate(qMaintainTask.startDateTime, searchable, "endDateTime"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("startDateTime"))) {
            where.and(super.afterOrEqualsDate(qMaintainTask.endDateTime, searchable, "startDateTime"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("endDateTime"))) {
            where.and(super.beforeOrEqualsDate(qMaintainTask.endDateTime, searchable, "endDateTime"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("modelCode"))) {
            where.and(super.equalsStr(qMaintainTask.modelCode, searchable, "modelCode"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("orgCode"))) {
            where.and(super.equalsStr(qMaintainTask.orgCode, searchable, "orgCode"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("chargeUserId"))) {
            where.and(super.equalsLong(qMaintainTask.chargeUserId, searchable, "chargeUserId"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            where.and(super.equalsStr(qMaintainTask.status.stringValue(), searchable, "status"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("result"))) {
            where.and(super.equalsStr(qMaintainTask.result.stringValue(), searchable, "result"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("chargeUserId"))) {
            where.and(super.equalsLong(qMaintainTask.chargeUserId, searchable, "chargeUserId"));
        }
        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return MaintainTask.class;
    }

}
